package com.machine.filling.function.init.activity;

import android.content.Intent;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.machine.filling.R;
import com.machine.filling.base.baseMvp.BaseActivity;
import com.machine.filling.db.dao.ClientInfoDao;
import com.machine.filling.function.init.INextFragment;
import com.machine.filling.function.init.InputOilGunInfo.InputOilGunInfoFragment;
import com.machine.filling.function.init.InputOilStationNum.InputOilStationNumFragment;
import com.machine.filling.function.init.OilGunList.OilGunListFragment;
import com.machine.filling.function.init.login.LoginFragment;
import com.machine.filling.function.main.activity.MainFuelActivity;
import com.machine.filling.function.set.SetActivity;
import com.machine.filling.function.set.SetFragment;
import com.machine.filling.function.set.orderList.OrderListFragment;
import com.machine.filling.util.StringUtil;

/**
 * Created by Administrator on 2018/1/11 0011.
 */

public class MainFrameActivity extends BaseActivity<MainFrameContract.Presenter> implements MainFrameContract.View {

    private FrameLayout framelayout;
    private FragmentManager fm;

    @Override
    public View initViews() {
        setContentView(R.layout.activity_main);
        framelayout = (FrameLayout) findViewById(R.id.framelayout);
        return null;
    }

    @Override
    public void initDatas() {
        //是否来自set界面, 是则显示登录; 否则显示主页或登录
        boolean isFromSet = mIntent.getBooleanExtra("isFromSet", false);
        //登录后需要跳转的页面类名称
        boolean isFillerLogin = mIntent.getBooleanExtra("isFillerLogin",false);

        fm = getSupportFragmentManager();
        //通过判断是否有token令牌,来判断是否已经完成初始化
        String token = ClientInfoDao.getInstance().getModleByDeviceNum().getToken();

        if (!isFromSet) {
            if (StringUtil.isNullOrEmpty(token)) {
                showLogin(new Intent());
            } else {
                showMain(new Intent());
            }
        } else {
            Intent intent = new Intent();
            intent.putExtra("isCanCancle", true);
            intent.putExtra("isFillerLogin", isFillerLogin);
            showLogin(intent);
        }
    }

    /**
     * 更换fragment
     *
     * @param fragment
     */
    private void replaceFragment(Fragment fragment) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.framelayout, fragment).commit();
    }

    /**
     * 显示登录界面
     */
    private void showLogin(final Intent intentPar) {
        LoginFragment loginFragment = new LoginFragment(intentPar);
        loginFragment.setINextFragment(new INextFragment() {
            @Override
            public void replaceNext(Intent intent, String className) {
                if (className.equals(OilGunListFragment.class.getName())) {
                    //初始化过，直接进入到添加油枪信息界面
                    showAddOilGun(intent);
                } else if (className.equals(InputOilStationNumFragment.class.getName())) {
                    //未初始化过，进入商家信息查询界面
                    showOilStationNum(intent);
                } else if (className.equals(MainFuelActivity.class.getName()) ) {
                    //返回主界面
                    startActivity(new Intent(mActivity, SetActivity.class));
                    finish();
                }else if (className.equals(OrderListFragment.class.getName())){
                    //进入订单列表页面
                    Intent intentTo = new Intent(mActivity,SetActivity.class);
                    intentTo.putExtra("showClassName",OrderListFragment.class.getName());
                    startActivity(intentTo);
                    finish();
                }
            }
        });
        replaceFragment(loginFragment);
    }

    /**
     * 显示输入商家编号界面
     */
    private void showOilStationNum(final Intent intentPar) {
        InputOilStationNumFragment inputOilStationNumFragment = new InputOilStationNumFragment(intentPar);
        inputOilStationNumFragment.setINextFragment(new INextFragment() {
            @Override
            public void replaceNext(Intent intent, String className) {
                if (className.equals(LoginFragment.class.getName())) {
                    showLogin(intentPar);
                } else if (className.equals(OilGunListFragment.class.getName())) {
                    showAddOilGun(intent);
                }
            }
        });
        replaceFragment(inputOilStationNumFragment);
    }

    /**
     * 显示添加油枪界面
     */
    private void showAddOilGun(Intent intentPar) {
        OilGunListFragment oilGunListFragment = new OilGunListFragment(intentPar);
        oilGunListFragment.setINextFragment(new INextFragment() {
            @Override
            public void replaceNext(Intent intent, String className) {
                if (className.equals(InputOilGunInfoFragment.class.getName())) {
                    //添加油枪信息
                    showInputOilGunInfo(intent);
                } /*else if (className.equals(OilMainFragment.class.getName())) {
                    //完成初始化
                    showMain(intent);
                }*/ else if (className.equals(LoginFragment.class.getName())) {
                    //系统还原
                    showLogin(intent);
                }
            }
        });
        replaceFragment(oilGunListFragment);
    }

    /**
     * 显示添加油枪信息界面
     */
    private void showInputOilGunInfo(Intent intentPar) {
        InputOilGunInfoFragment inputOilGunInfoFragment = new InputOilGunInfoFragment(intentPar);
        inputOilGunInfoFragment.setINextFragment(new INextFragment() {
            @Override
            public void replaceNext(Intent intent, String className) {
                if (className.equals(OilGunListFragment.class.getName())) {//增加和修改
                    showAddOilGun(intent);
                } else if (className.equals(OilGunListFragment.class.getName())) {//取消返回
                    showAddOilGun(intent);
                }
            }
        });
        replaceFragment(inputOilGunInfoFragment);
    }

    /**
     * 显示主页面
     */
    private void showMain(Intent intentPar) {
        startActivity(new Intent(mContext, MainFuelActivity.class));
        finish();
//        //主流
//        OilMainFragment oilMainFragment = new OilMainFragment(intentPar);
//        oilMainFragment.setINextFragment(new INextFragment() {
//            @Override
//            public void replaceNext(Intent intent, String className) {
//                if (className.equals(SetActivity.class.getName())) {
//                    MainFrameActivity.this.startActivity(new Intent(mActivity, SetActivity.class));
//                }
//            }
//        });
//        replaceFragment(oilMainFragment);
    }

    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏, 但仍然可以显示
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

//        Log.d("", "onNewIntent() returned: " + intent.getIntExtra("test",12345));
    }


}
